package cn.edu.scau.cmi.huangxiyan.client;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import cn.edu.scau.cmi.huangxiyan.singleton.Chairman;

public class SingletonClient extends Application {
	private Chairman chairman = null;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("SingletonClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		TextField textField = new TextField("请输入主席的名称");

		TextArea textArea = new TextArea("");
		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button button = new Button("创建");
		button.setPrefSize(200, 25);
		button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				chairman = Chairman.getInstance(textField.getText());
				if (textField.getText().equals(chairman.getName())) {
					textArea.appendText("名称：" + chairman.getName() + " 对象：" + chairman + "\n");
				} else {
					textArea.appendText(chairman.getName() + "主席已存在，不能再创建" + textField.getText() + "主席！\n");
				}
			}
		});
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(button);

		vBox.getChildren().addAll(textField, textArea, borderPane);
		Scene scene = new Scene(vBox, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
